/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.utils;

import ca.trader.comment.Comment;
import ca.trader.except.LocationNotFoundException;
import ca.trader.regions.Country;
import ca.trader.regions.Location;
import ca.trader.regions.Region;
import ca.trader.sale.Sale;
import ca.trader.sale.SaleCategoryEnum;
import ca.trader.startup.ServletContextManager;
import ca.trader.user.User;
import java.math.BigDecimal;

/**
 *
 * @author Ahsan
 */
public class ObjectFactory
{

	 /**
	  * Factory method for creating a user. 
	  * @param email the email of the user
	  * @param firstName the first name of the user
	  * @param lastName the last name of the user
	  * @param cryptedPassword the crypted password of the user
	  * @param activated whether the user should be set activated
	  * @param country the name of the country
	  * @param region the name of the region
	  * @param city the name of city
	  * @return the user object
	  * @throws LocationNotFoundException if the location is invalid
	  */
	 public static User createUser(String email, String firstName, String lastName, String cryptedPassword, boolean activated, String country, String region, String city) throws LocationNotFoundException
	 {
		  User user = new User();
		  user.setEmail(email);
		  user.setFirstName(firstName);
		  user.setLastName(lastName);
		  user.setLocation(createLocationFromNames(country, region, city));
		  user.setCryptedPassword(cryptedPassword);
		  

		  user.setActivated(activated);
		  

		  return user;
	 }

	 /**
	  * Creates a location object.
	  * @param countryName the name of the country
	  * @param regionName the name of the region
	  * @param cityName the name of the city
	  * @return the location object
	  * @throws LocationNotFoundException if the location is invalid
	  */
	 public static Location createLocationFromNames(String countryName, String regionName, String cityName) throws LocationNotFoundException
	 {
		  // Get the country by the matching name
		  Country country = ServletContextManager.getRegionManager().getCountry(countryName);
		  
		  // If no country was  returned, it means the country does not exist.  Throw exception
		  if (country == null)
		  {
			   throw new LocationNotFoundException ("Country " + countryName + " does not exist.");
		  }
			   
		  // Get the region with the matching name
		  Region region = country.getRegion(regionName);
		  
		  // If no region was  returned, it means the region does not exist.  Throw exception		  		  
		  if (region == null)
		  {
			   throw new LocationNotFoundException ("Region " + regionName + " does not exist.");
		  }
			

		  // Check that the region contains the city with the matching name
		  if (!region.containsCity(cityName))
		  {
			   	throw new LocationNotFoundException ("City " + cityName + " does not exist.");
		  }
		  
		  // Return the location
		  return new Location (country, region, cityName);
	 }
	 
	 /**
	  * Creates a sale object
	  * @param seller the user who is selling the object
	  * @param saleCategory the category of the sale
	  * @param saleTitle the title of the sale
	  * @param saleDesc the description of the sale
	  * @param salePrice the price of the sale
	  * @param saleLocation the location of the sale
	  * @return the sale object
	  */
	 public static Sale createSale (User seller, SaleCategoryEnum saleCategory, String saleTitle, String saleDesc, BigDecimal salePrice, Location saleLocation)
	 {
		  Sale sale = new Sale ();
		  
		  sale.setSeller(seller);
		  sale.setDescription(saleDesc);
		  sale.setTitle(saleTitle);
		  sale.setCategory(saleCategory);
		  sale.setPrice(salePrice);
		  sale.setLocation(saleLocation);
		  
		  return sale;
	 }
	 
	 public static Comment createComment (User commenter, Sale sale, String commentString)
	 {
		  Comment comment = new Comment();
		  
		  comment.setCommentContent(commentString);
		  comment.setCommentor(commenter);
		  comment.setSale(sale);
		  
		  sale.addComment(comment);
		  commenter.addComment(comment);
		  
		  return comment;
	 }

}
